package com.eascs.afterloan.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.api.form.O2oAfterLoanForm;
import com.eascs.afterloan.service.O2oAfterService;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.service.sys.SysOrgService;

@RestController
@RequestMapping("/rafterloan")
public class O2oAfterLoanRestController {
	@Autowired
	O2oAfterService service;
	@Autowired
	O2oDataListService o2oDataListService;
	@Reference
	SysOrgService sysOrgService;
	@RequestMapping("/o2o")
	public PaginationRestResult o2o(O2oAfterLoanForm entity) {
		String o2ourl = "/rafterloan/o2o";
		if (entity != null) {
			String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
			String bankLoan = entity.getBankLoan() == null ? "" : entity.getBankLoan();
			String operName = entity.getOperName() == null ? "" : entity.getOperName();
			String loanUserName = entity.getLoanUserName() == null ? "" : entity.getLoanUserName();
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String createUserName = entity.getCreateUserName() == null ? "" : entity.getCreateUserName();
			String createDateStart = entity.getCreateDateStart() == null ? null : entity.getCreateDateStart();
			String createDateEnd = entity.getCreateDateEnd() == null ? null : entity.getCreateDateEnd();
			String dateDueStart = entity.getDateDueStart() == null ? null : entity.getDateDueStart();
			String dateDueEnd = entity.getDateDueEnd() == null ? null : entity.getDateDueEnd();
			String ltdName = entity.getLtdName() == null ? "" : entity.getLtdName();
			String ltd = entity.getLtd() == null ? "" : entity.getLtd();
			String loanType = entity.getLoanType() == null ? "" : entity.getLoanType();
			String province = entity.getProvince() == null ? "" : entity.getProvince();
			String gdStatus = entity.getGdStatus() == null ? "" : entity.getGdStatus();
			String repaystatus = entity.getRepaystatus() == null ? "" : entity.getRepaystatus();
			String idc = entity.getIdc() == null ? "" : entity.getIdc();
			String regionalName = entity.getRegionalName()== null ? "" : entity.getRegionalName();
			String brandName = entity.getBrandName()== null ? "" : entity.getBrandName();
			String partnerName = entity.getPartnerName()== null ? "" : entity.getPartnerName();
			String hostProvince = entity.getHostProvince()== null ? "" : entity.getHostProvince();
			String firstLoanStartTime = entity.getFirstLoanStartTime() == null ? null : entity.getFirstLoanStartTime();
			String firstLoanEndTime = entity.getFirstLoanEndTime() == null ? null : entity.getFirstLoanEndTime();
			o2ourl = UrlUtil.appendUrl(o2ourl, "firstLoanStartTime", firstLoanStartTime);
			o2ourl = UrlUtil.appendUrl(o2ourl, "firstLoanEndTime", firstLoanEndTime);
			o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
			o2ourl = UrlUtil.appendUrl(o2ourl, "bankLoan", bankLoan);
			o2ourl = UrlUtil.appendUrl(o2ourl, "operName", operName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanUserName", loanUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createUserName", createUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateStart", createDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateEnd", createDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueStart", dateDueStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueEnd", dateDueEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltdName", ltdName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltd", ltd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanType", loanType);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "gdStatus", gdStatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "repaystatus", repaystatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "idc", idc);
			o2ourl = UrlUtil.appendUrl(o2ourl, "regionalName", regionalName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "brandName", brandName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerName", partnerName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "hostProvince", hostProvince);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		entity.setPageSize(10);
		//PageResult<Object[]> pageResult = service.getO2oAfterLoanByPage(entity);
		List<User> users=sysOrgService.checkDataPermission(UserHolder.getUser().getNo());
		String no="";
		if (users!=null) {
			for (User user : users) {
				no+=user.getNo()+",";
			}
		}
		if (!"".equals(no)) {
			if (",".equals(no.substring(no.length()-1, no.length()))) {
				no=no.substring(0, no.length()-1);
			}
		}
		entity.setUsersno(no);
		
		PageResult<Object[]> pageResult = service.getO2oAfterLoanByPageNew(entity);
		@SuppressWarnings("unused")
		List<Object[]> data  = pageResult.getData();
		//String titles[] = {"贷款申请编号", "放款银行", "产品类型", "经营平台","省区", "借款人姓名", "放款金额", "放款日期", "到期日期", "信贷经理","还款状态","归档状态","是否续贷","贷款次数"};
		String titles[] = {"贷款申请单号", "放款银行", "贷款类型", "经营平台","省区", "借款人姓名", "身份证", "放款金额(万元)", "放款日期", "到期日期", "信贷经理","还款状态","归档状态"};
		Button[] titleButtons = new Button[]{ButtonHelper.createOpenButton(null, "/detail?loanapplyNo=")};
		SelectDataOption[] selectDataOptions = { null,new SelectDataOption("BANKTYPEO2O"), new SelectDataOption("OTO_LOAN_TYPE"), null, null, null, null, null, null,null,null,new SelectDataOption("HKZT"), new SelectDataOption("PLACEONFILE")};
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setBuildPagingNavigation(true);
		o2oPagingInfo.setFirstRenderColumnIndex(1);
		//o2oPagingInfo.setTitleButtons(titleButtons);
		
		if(entity.getMenutype()==null){
			o2oPagingInfo.setTitleButtons(titleButtons);
		}
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		//o2oPagingInfo.setFormats(new Format[] { null, null, null, null,null, null , new DecimalFormat(",##0.00"),null, null, null,null,null });
		o2oPagingInfo.setFormats(new Format[] { null, null, null, null, null, null, null, new DecimalFormat(",##0.00"),null, null, null });
		o2oPagingInfo.setRendTo("#result_list");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
	}

	@RequestMapping("/oa")
	public PaginationRestResult oa(O2oAfterLoanForm entity) {
		String o2ourl = "/rafterloan/o2o";
		if (entity != null) {
			String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
			String bankLoan = entity.getBankLoan() == null ? "" : entity.getBankLoan();
			String operName = entity.getOperName() == null ? "" : entity.getOperName();
			String loanUserName = entity.getLoanUserName() == null ? "" : entity.getLoanUserName();
			String loanDateStart = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String loanDateEnd = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String createUserName = entity.getCreateUserName() == null ? "" : entity.getCreateUserName();
			String createDateStart = entity.getCreateDateStart() == null ? null : entity.getCreateDateStart();
			String createDateEnd = entity.getCreateDateEnd() == null ? null : entity.getCreateDateEnd();
			String ltdName = entity.getLtdName() == null ? "" : entity.getLtdName();
			String loanType = entity.getLoanType() == null ? "" : entity.getLoanType();
			
			o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
			o2ourl = UrlUtil.appendUrl(o2ourl, "bankLoan", bankLoan);
			o2ourl = UrlUtil.appendUrl(o2ourl, "operName", operName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanUserName", loanUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createUserName", createUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateStart", createDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateEnd", createDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltdName", ltdName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanType", loanType);
			o2ourl = o2ourl + "&oaIndex=";
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		PageResult<Object[]> pageResult = service.getOaAfterLoanByPage(entity);
		String titles[] = {""};
		PaginationInfo oaPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		ViewContext.put("oaPagingInfo", oaPagingInfo);
		ViewContext.put("o2ourl", o2ourl);
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(oaPagingInfo);
	}
	
	//提交测试
	
	@RequestMapping("credit")
	public PaginationRestResult credit(O2oAfterLoanForm entity) {
		String o2ourl = "/rafterloan/credit";
		if (entity != null) {
			
			String createDateStart = entity.getCreateDateStart() == null ? null : entity.getCreateDateStart();
			String createDateEnd = entity.getCreateDateEnd() == null ? null : entity.getCreateDateEnd();
			String coopereBank = entity.getCoopereBank() == null ? "" : entity.getCoopereBank();
			String province = entity.getProvince()== null ? "" : entity.getProvince();		
			String billCode = entity.getBillCode()== null ? "" : entity.getBillCode();			
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateStart", createDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateEnd", createDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "coopereBank", coopereBank);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		entity.setPageSize(10);
	
		PageResult<Object[]> pageResult = o2oDataListService.getCreditByPage(entity);
		@SuppressWarnings("unused")
		List<Object[]> data  = pageResult.getData();
		
		String titles[] = {"贷款单号","征信查询时间",  "借款人", "查询对象","姓名", "证件号码", "合作银行", "贷款类型", "银行状态", "证件类别", "省区","平台","经办人","归档状态"};
		Button[] titleButtons = new Button[]{ButtonHelper.createOpenButton(null, "/detail?billCode=")};
		
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setBuildPagingNavigation(true);
		o2oPagingInfo.setFirstRenderColumnIndex(0);
		o2oPagingInfo.setTitleButtons(titleButtons);
		o2oPagingInfo.setRendTo("#result_list");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
	}

	
	
}
